<? 

class Skin extends Root{
	var $Data;
	var $Beacon=0;
	var $BeaconTab;
	var $LangFile;

	function Skin(){
		$Num=$GLOBALS["Systeme"]->User->Skin;
		if (empty($Num)){
			$Num=$GLOBALS["Systeme"]->Conf->get("GENERAL::AUTH::MAIN_SKIN_NUM");
			$GLOBALS["Systeme"]->User->Skin = $Num;
		}
		$this->URL = "Skins/".$Num."/.cache/Skin.cache";
		$this->URLHEADER = "Skins/".$Num."/.cache/Header.cache";
		$this->URLINFO = "Skins/".$Num."/.cache/Skin.info";
		if (!$this->setConf($Num)) return false;
		if (!$this->checkIfModified()&&SKIN_CACHE){
			if (!$this->loadSkin()) $this->initSkin();
		}else {
			$this->initSkin();
		}
	}
	function get($Name){
		return $this->$Name;
	}
	function checkIfModified(){
		//RENVOIE VRAI SI MODIFICATON OU NON ACCES, FAUX SINON
		if (!file_exists($this->URLINFO)) return true;
		$Content=file($this->URLINFO);
		for ($i=0;$i<count($Content);$i++){
			$Ligne=rtrim($Content[$i]);
			$TabLigne=explode("||",$Ligne);
			if(filemtime($TabLigne[0])>$TabLigne[1]) return true;
		}
		return false;
	}

	function setConf($Num) {
		if (file_exists('Skins/'.$Num.'/Skin.conf')){
			//On met le XML sous forme de tableau.
			$Skin = new xml2array('Skins/'.$Num.'/Skin.conf');
			$tabSkin=$Skin->getResult();
			//On donne a la classe le titre+ le commentaire+ le bloc principal;
			$this->Titre = $tabSkin['SKIN']['#']['TITRE'][0]['#'];
			$this->Title = $tabSkin['SKIN']['#']['TITLE'][0]['#'];
			$this->Commentaire = $tabSkin['SKIN']['#']['COMMENTAIRE'][0]['#'];
			$this->ReplyTo = $tabSkin['SKIN']['#']['REPLY-TO'][0]['#'];
			$this->Description = $tabSkin['SKIN']['#']['DESCRIPTION'][0]['#'];
			$this->Keywords = $tabSkin['SKIN']['#']['KEYWORDS'][0]['#'];
			$this->Classification = $tabSkin['SKIN']['#']['CLASSIFICATION'][0]['#'];
			$this->bloc = $tabSkin['SKIN']['#']['BLOC'][0]['#'];
			$this->Frameset = $tabSkin['SKIN']['#']['FRAMESET'][0];
			if ($GLOBALS["Systeme"]->LangageDefaut!=$GLOBALS["Systeme"]->DefaultLanguage)
				if($tabSkin['SKIN']['#'][$GLOBALS["Systeme"]->Language[$GLOBALS["Systeme"]->DefaultLanguage].'-DEFAULT_PAGE'][0]['#']!="")$this->DefaultPage = $tabSkin['SKIN']['#'][$GLOBALS["Systeme"]->Language[$GLOBALS["Systeme"]->DefaultLanguage].'-DEFAULT_PAGE'][0]['#'];
				if($tabSkin['SKIN']['#'][$GLOBALS["Systeme"]->Language[$GLOBALS["Systeme"]->DefaultLanguage].'-LANG'][0]['#']!="")$this->LangFile =$tabSkin['SKIN']['#'][$GLOBALS["Systeme"]->Language[$GLOBALS["Systeme"]->DefaultLanguage].'-LANG'][0]['#'];
			else $this->DefaultPage = $tabSkin['SKIN']['#']['DEFAULT_PAGE'][0]['#'];
			if (is_object($GLOBALS["Systeme"]->Header)){
				//Configuration de l entete
				$GLOBALS["Systeme"]->Header->setTitle($this->Titre);
				$GLOBALS["Systeme"]->Header->setKeywords($this->Keywords);
				$GLOBALS["Systeme"]->Header->setDescription($this->Description);
				$GLOBALS["Systeme"]->Header->setFrameset($this->Frameset);
				$GLOBALS["Systeme"]->Header->setClassification($this->Classification);
				$GLOBALS["Systeme"]->Header->setReplyTo($this->ReplyTo);
			}
			//Configuration page par defaut si definie
			if ($this->DefaultPage!=NULL)$GLOBALS["Systeme"]->setDefault($this->DefaultPage);
		}else{
			echo 'Le fichier de la skin '.$Num.' n\'a pas &#233;t&#233; ouvert.<BR/>';
			//Si ce n'est la skin par defaut, on la charge (si ok: -1, sinon 0). Si c'est la skin par defaut, on renvoie 0 car son fichier de conf n'est pas ouvrable.
			if ($Num !=$GLOBALS['Systeme']->DefaultSkin){
				if ($this->initSkin($GLOBALS['Systeme']->DefaultSkin)){
					$GLOBALS['Systeme']->Skin = $GLOBALS['Systeme']->DefaultSkin;
					return -1;
				}else return 0;
			}else return 0;
		}
		return 1;
	}

	function initSkin(){
		//On essaye d'ouvrir le fichier skin, si &#231;a ne marche pas on appelle la skin 0
// 		echo "INIT SKIN";
		if (SKIN_CACHE){
			//Creation des fichiers du cache de la skin
			$Url="Skins/".$GLOBALS['Systeme']->User->Skin."/.cache";
			if (!is_dir($Url)){
				mkdir($Url);
			}
			//On genere le html
			$SkinBloc=new Bloc();
			$Process = new Process("Skin");
			$Beacon["BEACON"] = "BLOC";
			$SkinBloc->setFromVar($this->bloc,"[DATA]",$Beacon);
			$SkinBloc->init($Process);
			$this->SkinObjects = $SkinBloc;
			$this->WriteHeaderCache($Url."/Header");
			$this->Data = $this->SkinObjects->Affich();
			//Ensuite on genere les structure conditionnelle 
			$SkinBloc=new Bloc();
			$Process = new Process("Struct");
			$SkinBloc->loadData($this->Data);
			$this->Data="";
			$SkinBloc->init($Process);
			$this->SkinObjects = $SkinBloc;	
			$ObjVars = $this->SkinObjects->getObjVars();
			$this->SkinObjects->ObjVars = $ObjVars;
			//On ecrit le cache
			$this->SkinObjects->writeCache($Url);
			//Generatio ndu fichier info
			$this->WriteSkinInfo();
			$this->SkinObjects = "";
			$this->loadSkin();

		}else{
// 			echo "SKIN INIT \r\n";
			$SkinBloc=new Bloc();
			$Process = new Process("Skin");
			$Beacon["BEACON"] = "BLOC";
			$SkinBloc->setFromVar($this->bloc,"[DATA]",$Beacon);
			$SkinBloc->init($Process);
			$this->SkinObjects = $SkinBloc;
			$this->Data = $this->SkinObjects->Affich();
			$this->loadSkin();
// 			echo "SKIN INIT OK\r\n";
		}
	}
	

	function writeSkinInfo(){
		$BlockList=$GLOBALS['Systeme']->BlockList;
		if (!$File=fopen ($this->URLINFO,"w"))return false;
		for ($i=0;$i<count($BlockList);$i++){
			$Entree = $BlockList[$i]->Path.'||';
			$Entree.=filemtime($BlockList[$i]->Path);
			$Entree.="\n";
			fwrite ($File,$Entree);
		}
		fclose($File);

		return true;
	}

	function WriteHeaderCache($Url){
		//Recuperation de l entete
		$Entete = $GLOBALS["Systeme"]->Header;
		//On ecrit les fichiers 
		$this->writeCacheFile(serialize($Entete),$Url.".cache");
	}


	function processCache() {
		//On ecrit le fichier de cache
			$Cache = $this->Data;
			$Cache = $this->ProcessingCache($Cache);
			$this->BeaconTab=Array();
			$this->Beacon = 0;
			$this->writeCacheFile($Cache,$this->URL);
 	}

	function loadSkin() {
// 		echo "LOAD SKIN ".$this->URL."\r\n";
		if (SKIN_CACHE) {
			if (!file_exists($this->URL)) return false;
			//Donc on charge la skin
			$this->SkinObjects= unserialize(file_get_contents($this->URL));
		}else{
			$this->SkinObjects = new Bloc();
			$Data = $this->Data;
			$this->SkinObjects->loadData($Data,"SkinBase");
		}
		return true;
	}

	function Generate($Process) {
		if (SKIN_CACHE) {
			$GLOBALS["Systeme"]->Log->log("SKIN >> GENERATE START");
			$Process = $this->SkinObjects->Generate($Process,true);
			$this->Data = $this->SkinObjects->Affich();
			return $Process;
		}else{
			$this->SkinObjects->init($Process);
			$Process = $this->SkinObjects->Generate($Process,true);
			$this->Data = $this->SkinObjects->Affich();
			return $Process;
		}
	}

	//AJOUTER UN FICHIER NOM_DE_LA_LANGUE.lang a la racine de la skin et specifier le fichier dans la config ex:<EN-LANG>Skins/Pragma/Anglais.lang</EN-LANG>
	function ProcessLang($data) {
		//On ouvre le fichier de langue si il existe
		if (!empty($this->LangFile)&&file_exists($this->LangFile)){
			$f =fopen($this->LangFile,"r");
			while (!feof($f)){
				$p = fgets($f,4096);
				$p = preg_replace("#
#","",$p);
				$p = explode("|",$p);
				$data = preg_replace("#".$p[0]."#",$p[1],$data);
			}
		}
		//Pour chaque paire on remplace l'exuivalent dans le contenu
		return $data;
	}
		

	function writeCacheFile($Data,$Url) {
//		$Url = str_replace("/","_",$Url);
		if (!$File=fopen ($Url,"w"))return false;
		fwrite($File,$Data);
		fclose($File);
	}

	function Affich($Data="") {
		return $this->Data;
	}
}

?>